Category adder/Category.py
!/usr/bin/python # -*- coding: utf-8 -*- __author__ = 'User:TyA' __version__ = '2.0' print """ Welcome to the Semi-automatic Category Adder! This program allows you to specify a wiki, namespace, and category and then \nquickly list pages to add to that category. This is particulary useful when you're on and \nwant to add a lot of pages to a category quickly. To report any bugs, please to go http://c.wikia.com/wiki/Message_Wall:TyA """ from simplemediawiki import MediaWiki import getpass import sys def getMode(): mode = raw_input("What mode would you like to use? (c for categories; t for templates): ") #mode = mode.lower() return mode def getUserName(): username = raw_input("Username: ") # Gets user's username return username def getUserPass(): password = getpass.getpass() #Gets user's password return password def getContent(page): # Makes the function to get page content content = wiki.call({'action':'query','format':'json','prop':'revisions','rvprop':'content','titles':page}) try: content = content"query""pages" return content[content.keys()0]"revisions"0'*' except: print "Page does not exist" content = "" return content def saveEdit(page, content): query = {"action":"edit", "title":page, "summary":summary, "text":content, "token":edittoken} # Creates the API query try: wiki.call(query) # Tries to make the query except: print "Error" def edit(page, edittoken): # Makes the edit function page = namespace + page # Makes the Full page name content = getContent(page) if mode is "c": if ("Category:" + dataName + "" in content): print "Category is already on the page - Skipping" elif (content is ""): content = "Category:" + dataName + "" print content saveEdit(page, content) else: content = content + "\nCategory:" + dataName + "" print content saveEdit(page, content) elif mode is "t": #if (" " print content saveEdit(page, content) else: content = content + "\n " print content saveEdit(page, content) def getEditToken(): query = {"action":"query", "prop":"info", "intoken":"edit", "titles":"Main Page"} #The query to request an edit token q = wiki.call(query)"query""pages" # Making the call for i in q: try: # Trying wiki.edittoken = qi"edittoken" return wiki.edittoken except: # no edit privilege for us wiki.edittoken = None; print "Failed to get edit token!" return False def start(): counter = 1 while counter is 1: page = raw_input("Page: ") ## TODO: Make this work, currently it only does the else: statement if page.lower() is "change category": category = raw_input("New Category: ") print "Category changed!" elif page.lower() is "change namespace": namespace = raw_input("New Namespace: ") print "Namespace changed!" elif page.lower() is "change summary": summary = raw_input("New Summary: ") print "Summary changed!" elif page.lower() is "change mode": mode = raw_input("New Mode: ") print "Mode changed to " + mode elif page.lower() is "quit": print "Goodbye." sys.exit() else: edit(page, edittoken) url = raw_input("Sitename: ") # Gets the API.php url url = "http://" + url + ".wikia.com/api.php" wiki = MediaWiki(url) username = getUserName() password = getUserPass() print "Attempting to Login to " + url # Prints where it's trying to login to if (wiki.login(username, password) is True): #Attempts to login to the wiki print "Login successful!" # If successful else: print "Login failed." # If fails username = getUserName() password = getUserPass() if (wiki.login(username, password) is True): print "Login successful!" start() else: print "Login failed. Terminating... " sys.exit() summary = raw_input("Summary: ") # Gets the summary namespace = raw_input("Namespace: ") # Gets the starting namespace edittoken = getEditToken() mode = getMode() if mode is "c": dataName = raw_input("Category: ") elif mode is "t": dataName = raw_input("Template: ") start()